package com.zhangxu.microservice.system.web.controller.ae;

import com.zhangxu.microservice.common.result.CommonResult;
import com.zhangxu.microservice.system.biz.dto.BatchUploadResourceTreeDTO;
import com.zhangxu.microservice.system.biz.dto.ResourceTreeDTO;
import com.zhangxu.microservice.system.biz.service.ResourceService;
import com.zhangxu.microservice.system.web.convert.ResourceWebConverter;
import com.zhangxu.microservice.system.web.request.BatchUploadResourceTreeReq;
import com.zhangxu.microservice.system.web.response.ResourceTreeResp;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 资源管理控制器
 */
@Slf4j
@RestController
@RequestMapping("/ae/resources")
@RequiredArgsConstructor
@Tag(name = "资源管理", description = "资源管理相关接口")
public class ResourceController {

    private final ResourceService resourceService;
    private final ResourceWebConverter resourceWebConverter;

    @Operation(summary = "批量上传资源树", description = "批量上传系统权限资源树")
    @PostMapping("/batch-upload")
    public CommonResult<Void> batchUploadResourceTree(@RequestBody @Validated BatchUploadResourceTreeReq req) {
        BatchUploadResourceTreeDTO DTO = resourceWebConverter.toBatchUploadResourceTreeDTO(req);
        resourceService.batchUploadResourceTree(DTO);
        return CommonResult.success();
    }

    @Operation(summary = "获取资源树", description = "获取系统资源权限树")
    @GetMapping("/tree")
    public CommonResult<List<ResourceTreeResp>> getResourceTree(@Parameter(description = "系统ID", example = "1") @RequestParam(value = "appId", required = false) Long appId) {
        List<ResourceTreeDTO> resourceTreeDTOList = resourceService.getResourceTree(appId);
        List<ResourceTreeResp> resourceTreeRespList = resourceWebConverter.toResourceTreeRespList(resourceTreeDTOList);
        return CommonResult.success(resourceTreeRespList);
    }
}